home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / opengl_logo / opengl_logo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.7 KB  |  134 lines

  1. /*
  2.  * Main.c
  3.  *
  4.  * This file is part of the openGL-logo demo.
  5.  * (c) Henk Kok (kok@wins.uva.nl)
  6.  *
  7.  * Copying, redistributing, etc is permitted as long as this copyright
  8.  * notice and the Dutch variable names :) stay in tact.
  9.  */
  10.  
  11. #include <GL/glut.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <math.h>
  15.  
  16. GLfloat lightpos[4] = { 1.0, 1.0, 1.0, 0.0 };
  17. GLfloat lightamb[4] = { 0.3, 0.3, 0.3, 1.0 };
  18. GLfloat lightdif[4] = { 0.8, 0.8, 0.8, 1.0 };
  19. float speed=0, progress = 1;
  20. void SetCamera(void);
  21.  
  22. extern void randomize(void);
  23. extern void def_logo(void);
  24. extern void draw_logo(void);
  25.  
  26. void do_display (void)
  27. {
  28.     SetCamera();
  29.     draw_logo();
  30.     glFlush();
  31.     glutSwapBuffers();
  32. }
  33.  
  34. void display(void)
  35. {
  36.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  37.     do_display();
  38. }
  39.  
  40. void myinit (void)
  41. {
  42.     glShadeModel (GL_SMOOTH);
  43.     glEnable(GL_DEPTH_TEST);
  44.     glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  45.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightamb);
  46.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightdif);
  47.     glEnable(GL_LIGHTING);
  48.     glEnable(GL_LIGHT0);
  49.     glColor3f(1.0, 1.0, 1.0);
  50.     glClearColor(0.0, 0.0, 0.0, 1.0);
  51.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  52.     glEnable(GL_NORMALIZE);
  53.     def_logo();
  54.     glCullFace(GL_BACK);
  55.     glEnable(GL_CULL_FACE);
  56. }
  57.  
  58. /* ARGSUSED1 */
  59. void parsekey(unsigned char key, int x, int y)
  60. {
  61.     switch (key)
  62.     {
  63.     case 27: exit(0); break;
  64.     case 13: break;
  65.     case ' ': progress = 1; randomize(); break;
  66.     }
  67. }
  68.  
  69. /* ARGSUSED1 */
  70. void parsekey_special(int key, int x, int y)
  71. {
  72.     switch (key)
  73.     {
  74.     case GLUT_KEY_UP:        break;
  75.     case GLUT_KEY_DOWN:        break;
  76.     case GLUT_KEY_RIGHT:    break;
  77.     case GLUT_KEY_LEFT:        break;
  78.     }
  79. }
  80.  
  81. void Animate(void)
  82. {
  83.     speed = -0.95*speed + progress*0.05;
  84.     if (progress > 0.0 && speed < 0.0003)
  85.         speed = 0.0003;
  86.     if (speed > 0.01)
  87.         speed = 0.01;
  88.     progress = progress - speed;
  89.     if (progress < 0.0)
  90.     {
  91.         progress = 0.0;
  92.         speed = 0;
  93.     }
  94.     glutPostRedisplay();
  95. }
  96.  
  97. void myReshape(int w, int h)
  98. {
  99.     glMatrixMode (GL_MODELVIEW);
  100.     glViewport (0, 0, w, h);
  101.     glLoadIdentity();
  102.     SetCamera();
  103. }
  104.  
  105. void SetCamera(void)
  106. {
  107.     glMatrixMode (GL_PROJECTION);
  108.     glLoadIdentity ();
  109.     glFrustum (-0.1333, 0.1333, -0.1, 0.1, 0.2, 150.0);
  110.     glMatrixMode(GL_MODELVIEW);
  111.     glLoadIdentity();
  112.     gluLookAt(0,1.5,2, 0,1.5,0, 0,1,0);
  113.     glTranslatef(0.0, -8.0, -45.0);
  114.     glRotatef(-progress*720, 0.0, 1.0, 0.0);
  115. }
  116.  
  117. int main(int argc, char *argv[])
  118. {
  119.     glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
  120.     glutInitWindowPosition(200, 0);
  121.     glutInitWindowSize(640, 480);
  122.     glutCreateWindow("Rotating OpenGL Logo");
  123.     glutDisplayFunc(display);
  124.     glutKeyboardFunc(parsekey);
  125.     glutSpecialFunc(parsekey_special);
  126.     glutReshapeFunc(myReshape);
  127.     glutIdleFunc(Animate);
  128.     randomize();
  129.     myinit();
  130.     glutSwapBuffers();
  131.     glutMainLoop();
  132.     return 0;             /* ANSI C requires main to return int. */
  133. }
  134.